package com.gimi.cloud.jimi.client.service.userInfo.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.gimi.cloud.jimi.common.utils.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gimi.cloud.jimi.client.service.userInfo.WorkInfoService;
import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.dao.dao.User.WorkInfoDao;
import com.gimi.cloud.jimi.dto.userInfo.RdWorkInfoDTO;
import com.gimi.cloud.jimi.entity.userInfo.RdWorkInfoEntity;
@Service
@Transactional
public class WorkInfoServiceImpl implements WorkInfoService {
	
	@Autowired
	private WorkInfoDao workInfoDao;

	@Override
	public void addWorkInfo(RdWorkInfoDTO workInfoDTO) {
		RdWorkInfoEntity record = new RdWorkInfoEntity();
		//TODO 有效期定义
		try {
			workInfoDTO.setReportEffectionEnd(DateUtil.strtoDate("2099-01-01","yyyy-MM-dd"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		BeanUtils.copyProperties(workInfoDTO, record);
		int i = workInfoDao.insert(record);
		if (i <= 0) {
			throw new MsgException(ResponseCodeEnum.INVALID,"新增失败!");
		}
	}

	@Override
	public RdWorkInfoDTO getWorkInfoSingle(RdWorkInfoDTO workInfoDTO) {
		RdWorkInfoEntity workInfoEntity = workInfoDao.selectByUserId(workInfoDTO.getUserId());
		RdWorkInfoDTO dto = new RdWorkInfoDTO();
		if(workInfoEntity != null) {
			BeanUtils.copyProperties(workInfoEntity, dto);
		}
		return dto;
	}

	@Override
	public void updateWorkInfo(RdWorkInfoDTO workInfoDTO) {
		RdWorkInfoEntity record = new RdWorkInfoEntity();
		BeanUtils.copyProperties(workInfoDTO, record);
		int i = workInfoDao.updateByPrimaryKeySelective(record);
		if (i <= 0) {
			throw new MsgException(ResponseCodeEnum.INVALID,"更新失败!");
		}
	}

	@Override
	public RdWorkInfoDTO getWorkInfoByUserId(Integer userId) {
		RdWorkInfoEntity workInfoEntity = workInfoDao.selectByUserId(userId);
		RdWorkInfoDTO dto = new RdWorkInfoDTO();
		BeanUtils.copyProperties(workInfoEntity, dto);
		return dto;
	}

	@Override
	public void deleteWorkInfo(RdWorkInfoDTO workInfoDTO) {
		int i = workInfoDao.deleteByInfoId(workInfoDTO.getInfoId());
		if (i <= 0) {
			throw new MsgException(ResponseCodeEnum.INVALID,"删除失败!");
		}
	}

	@Override
	public Map<String, Object> getAuthenticationInfo(Integer userId) {
		Map<String, Object> map = new HashMap<>();
		RdWorkInfoEntity entity = workInfoDao.selectByUserId(userId);
		//无数据
		if (entity == null) {
			map.put("status", 0);
			return map;
		}
		RdWorkInfoDTO effectionWorkInfo = workInfoDao.isEffection(userId);
		//有数据未过期
		if (entity != null && effectionWorkInfo != null ) {
			map.put("status", 1);
			map.put("data", effectionWorkInfo);
			return map;
		}else {
			map.put("status", 2);
			return map;
		}
		
	}

}
